<?php

/*
 * CONTROLE FINANÇAS PESSOAIS
 *
 * Controle dos lançamentos de contas
 *
 * Histórico:
 *		v. 12/09/2011, EDGARD FANTINI JÚNIOR - efantinij@gmail.com
 *			- versão inicial
 *
 * FRAMWORK: edframe - Copyleft (cc) 2012 EDMAR Sistemas
 *
 */

$paginaSolicitada = 'Financas';
$segura = 'USC.CO.10.00' ;
$descricao = "CONTROLE DE LAN&Ccedil;AMENTOS DE CONTAS";


function contasInicio ()
{
	try
	{
		TTransaction::open($_SESSION['infoCon']);
		TTransaction::setLogger(new TLoggerTXT('/tmp/contasInicio.txt'));

		$res .="<div style='height:500px;  overflow:auto'>";
		$res .= "<TABLE class='relat1'>" ;
		$res .= "<TR>" ;
		$res .= "<TD width='20%'></TD>" ;
		for ($x = -2 ; $x < 14 ; $x ++)
		{
			$mesPesq = date ("m",mktime (0,0,0,date("m")+$x,date("d"),date("Y"))) ;
			$anoPesq = date ("Y",mktime (0,0,0,date("m")+$x,date("d"),date("Y"))) ;

			$mesRef = new mesref ;
			$mesRef->nome = $mesPesq."/".$anoPesq ;
			$mesRef->ativo = 'A' ;
			$mesRef->colunas = "mref" ;
			$_MesRef = $mesRef->mesref ;

			if (empty($_MesRef))
				throw new Exception("Mes de vencimento n&atilde;o cadastrado.") ;

			$res .= "<TD class='".set_class($x)."' width='20%' colspan='2'>" ;
			$res .= "<A href='#' onclick='acao(3,".$_MesRef[0]->mref.")' >" ;
			$res .= str_pad(mes($mesPesq)."/".$anoPesq,20," ",STR_PAD_BOTH) ;
			$res .= "</A>" ;
			$res .= "</TD>" ;


			$th .= "<TD class='".set_class($x)."' >PAGAR</TD>" ;
			$th .= "<TD class='".set_class($x)."' >PLANEJ.</TD>" ;

			$compMes = new opera ;
			$compMes->mesref = $_MesRef[0]->mref ;
			$compMes->usuario = $_SESSION['userId'] ;
			$compMes->tipo = "D" ;
			$compMes->ativo = 'A' ;
			$compMes->colunas = 'sum(valor) as soma' ;
			$_Valores = $compMes->opera ;
			$resSum .= "<TD class='".set_class($x)."'>".$_Valores[0]->soma."</TD>" ;

			$pag = new pagante ;
			$pag->mesref = $_MesRef[0]->mref ;
			$pag->usuario = $_SESSION['userId'] ;
			$pag->ativo = 'A' ;
			$pag->colunas = 'sum(valor) as soma' ;
			$_Pagant = $pag->pagante ;
			$resSum .= "<TD class='".set_class($x)."' >".$_Pagant[0]->soma."</TD>" ;

			$credMes = new opera ;
			$credMes->mesref = $_MesRef[0]->mref ;
			$credMes->usuario = $_SESSION['userId'] ;
			$credMes->tipo = "C" ;
			$credMes->ativo = 'A' ;
			$credMes->colunas = 'sum(valor) as soma' ;
			$_ValoreCred = $credMes->opera ;
			$resCred .= "<TD colspan='2' class='".set_class($x)."' >".$_ValoreCred[0]->soma."</TD>" ;

			$resSaldo .= "<TD class='".set_class($x)."'>".($_ValoreCred[0]->soma - $_Valores[0]->soma)."</TD>" ;
			$resSaldo .= "<TD class='".set_class($x)."'>".($_ValoreCred[0]->soma - $_Pagant[0]->soma)."</TD>" ;


		}
		$res .= "</TR><TR><TD></TD>".$th."</TR>";

		$a = new titulo;
		$a->ativo = "A" ;
		$a->tipo = "D" ;
		$a->colunas = "tit, nome" ;
		$_Titulos = $a->titulo ;

		$i = 0 ;
		foreach ($_Titulos as $userFormpag)
		{
			$res .= "<TR>" ;
			$res .= "<TD>".$userFormpag->nome ."</TD>" ;

			for ($x = -2 ; $x < 14 ; $x ++)
			{
				$mesPesq =
				$anoPesq = date ("Y",mktime (0,0,0,date("m")+$x,date("d"),date("Y"))) ;

				$mesRef = new mesref ;
				$mesRef->nome = date ("m/Y",mktime (0,0,0,date("m")+$x,date("d"),date("Y"))) ;
				$mesRef->ativo = 'A' ;
				$mesRef->colunas = "mref" ;
				$_MesRef = $mesRef->mesref ;
				if (empty($_MesRef))
					throw new Exception("Mes de vencimento n&atilde;o cadastrado.") ;

				$b = new opera ;
				$b->mesref = $_MesRef[0]->mref ;
				$b->titulo = $userFormpag->tit ;
				$b->usuario = $_SESSION['userId'] ;
				$b->ativo = 'A' ;
				$b->colunas = 'sum(valor) as soma' ;
				$_Valores = $b->opera ;

				$pag = new pagante ;
				$pag->mesref = $_MesRef[0]->mref ;
				$pag->titulo = $userFormpag->tit ;
				$pag->usuario = $_SESSION['userId'] ;
				$pag->ativo = 'A' ;
				$pag->colunas = 'valor' ;
				$_Pagant = $pag->pagante ;


				$res .= "<TD class='".set_class($x)."'>" ;
				$res .= "<A href='#' onclick='acao(3,\"".$_MesRef[0]->mref."|".$userFormpag->tit."\")' >" ;
				$res .= $_Valores[0]->soma ;
				$res .= "</A></TD>" ;

				if (!$_Pagant[0]->valor)
					$_Pagant[0]->valor = '&nbsp;' ;
				$res .= "<TD class='".set_class($x)."'>".$_Pagant[0]->valor ."</TD>" ;

			}
			$i++ ;
			$res .= "</TR>" ;
		}
		$res .= "<TR><TH>CREDITOS</TH>".$resCred."</TR>" ;
		$res .= "<TR><TH>TOTAL</TH>".$resSum."</TR>" ;
		$res .= "<TR><TH>SALDO</TH>".$resSaldo."</TR>" ;
		$res .= "</TABLE>" ;
		$res .="</DIV>" ;



	}
	catch (Exception $e)
	{
		$_var = MformTryCache::objReturn($e) ;
		TTransaction::rollback() ;
		return $_var;
	}
	return $res ;
}


function buscaEdicaoConta($mref) // EDIÇÃO DO MES
{
	try
	{
		TTransaction::open($_SESSION['infoCon']);
		TTransaction::setLogger(new TLoggerTXT('/tmp/buscaEdicaoConta.txt'));

		$res[1] = '' ;
		if (!$mref)
		{
			$mesRef = new mesref ;
			$mesRef->nome = date ("m/Y",mktime (0,0,0,date("m"),date("d"),date("Y"))) ;
			$mesRef->ativo = 'A' ;
			$mesRef->colunas = "mref" ;
			$mref = $mesRef->mesref[0]->mref ;
		}

// APRESENTAR OPERAÇÕES DE DÉBITO - COMPRAS

		$mesTitulo = explode("|",$mref) ;
		if (count($mesTitulo) == 2)
		{
			$mref = $mesTitulo[0] ;
			$tit = $mesTitulo[1] ;

			$deb = new opera;
			$deb->usuario = $_SESSION['userId'] ;
			$deb->titulo = $mesTitulo[1] ;
			$deb->tipo = "D" ;
			$deb->ativo = 'A' ;
			$deb->mesref = $mesTitulo[0] ;
			$deb->colunas = 'op, datcompr, titulo, historico, parcerest, mesref, valor' ;
			$_Deb = $deb->opera ;

			$res[1] .= "<TABLE class= 'entrada'>" ;
			$res[1] .= "<CAPTION>COMPRAS: REF ".$_Deb[0]->mesrefNome."</CAPTION>" ;
			$res[1] .= "<TR>" ;
			$res[1] .= "<TH width='3%' >Sel</TH>" ;
			$res[1] .= "<TH width='8%' >Data</TH>" ;
			$res[1] .= "<TH width='27%' >T&iacute;tulo</TH>" ;
			$res[1] .= "<TH width='47%' >Hist&oacute;rico</TH>" ;
			$res[1] .= "<TH width='5%' >Vencimento</TH>" ;
			$res[1] .= "<TH width='5%' >Parcela</TH>" ;
			$res[1] .= "<TH width='5%' >Valor</TH>" ;
			$res[1] .= "</TR>" ;

			$i = 1 ;
			foreach ($_Deb as $deb)
			{
				$res[1] .= "<TR style='background-color:".set_cor($i)."'>" ;
				$res[1] .= "<TD>" ;
				$res[1] .= "<INPUT type='radio' name='selec' onClick='editarConta(".$deb->op.")' />" ;
				$res[1] .= "</TD>" ;
				$res[1] .= "<TD>".formatDataPort($deb->datcompr)."</TD>" ;
				$res[1] .= "<TD>".$deb->nomeTitulo."</TD>" ;
				$res[1] .= "<TD>".$deb->historico."</TD>" ;
				$res[1] .= "<TD>".$deb->mesrefNome."</TD>" ;
				$res[1] .= "<TD>".$deb->parcerest."</TD>" ;
				$res[1] .= "<TD>".$deb->valor."</TD>" ;
				$res[1] .= "</TR>" ;
				$i++ ;
			}
			$res[1] .= "<TR>" ;
			$res[1] .= "<TH colspan='7'>" ;
			$res[1] .= "<INPUT type='radio' name='selec' onClick='editarConta(0)' />Nova compra</TH>" ;
			$res[1] .= "</TR>" ;
			$res[1] .= "</TABLE>" ;
		}



// APRESENTAR RESUMO POR TÍTULOS

		$res[2] .= "<TABLE class='entrada'>" ;
		$res[2] .= "<CAPTION>PAGAMENTOS: REF ".$_Deb[0]->mesrefNome."</CAPTION>" ;
		$res[2] .= "<TR>" ;
		$res[2] .= "<TH>T&Iacute;TULO</TH>" ;
		$res[2] .= "<TH>Valor Efetivo</TH>" ;
		$res[2] .= "<TH>Valor PAGO</TH>" ;
		$res[2] .= "</TR>" ;

		$a = new titulo;
		$a->ativo = "A" ;
		$a->tipo = "D" ;
		$a->colunas = "tit, nome" ;
		$_Titulos = $a->titulo ;

		$i = 0 ;
		$dbitoMes = 0 ;
		foreach ($_Titulos as $titulo)
		{
			$compMes = new opera ;
			$compMes->mesref = $mref ;
			$compMes->titulo = $titulo->tit ;
			$compMes->usuario = $_SESSION['userId'] ;
			$compMes->tipo = "D" ;
			$compMes->ativo = 'A' ;
			$compMes->colunas = 'sum(valor) as soma' ;
			$_Valores = $compMes->opera ;

			$pag = new pagante ;
			$pag->mesref = $mref ;
			$pag->titulo = $titulo->tit ;
			$pag->usuario = $_SESSION['userId'] ;
			$pag->ativo = 'A' ;
			$pag->colunas = 'pg, valor' ;
			$_Pagant = $pag->pagante ;

			$res[2] .= "<TR style='background-color:".set_cor($i)."'>" ;
			$res[2] .= "<TD width='20%' >".$titulo->nome ;
			$res[2] .= "</TD>" ;

			$res[2] .= "<TD width='20%' >".$_Valores[0]->soma ;
			$res[2] .= "</TD>" ;

			$res[2] .= "<TD width='20%' >" ;
			$res[2] .= "<INPUT size='6' value='".$_Pagant[0]->valor."' name='".$_Pagant[0]->pg."' " ;
			$res[2] .= "tit='".$titulo->tit."' onkeypress='mascara(this,soFinanceiro)'/>" ;
			$res[2] .= "</TD>" ;

			$dif = $_Valores[0]->soma - $_Pagant[0]->valor ;

			$res[2] .= "<TD width='20%'  >" ;
			$res[2] .= "<INPUT size='6' name='juros_".$titulo->tit."'" ;
			$res[2] .= "onkeypress='mascara(this,soFinanceiro)'/>" ;
			$res[2] .= "</TD>" ;
			$res[2] .= "<td class='info' onclick='calcJuros(".$titulo->tit.",".$dif.")'></td>" ;
			$res[2] .= "<td id='dif_".$titulo->tit."' ></td>" ;
			$res[2] .= "<td id='jurosReal_".$titulo->tit."' ></td>" ;

			$dbitoMes += $_Pagant[0]->valor ;
			$res[2] .= "</TR>" ;
			$i++ ;
		}

		$credMes = new opera ;
		$credMes->mesref = $mref ;
		$credMes->usuario = $_SESSION['userId'] ;
		$credMes->tipo = "C" ;
		$credMes->ativo = 'A' ;
		$credMes->colunas = 'sum(valor) as soma' ;
		$_Valores = $credMes->opera ;

		if (empty($_Valores))
			$_Valores[0]->soma = 0 ;

		$res[2] .= "<TR>" ;
		$res[2] .= "<TH colspan='2' >CR&Eacute;DITOS NO M&Ecirc;S</TH>" ;
		$res[2] .= "<TH >".$_Valores[0]->soma."</TH>" ;
		$res[2] .= "</TR>" ;

		$res[2] .= "<TR>" ;
		$res[2] .= "<TH colspan='2' >D&Eacute;BITOS NO M&Ecirc;S</TH>" ;
		$res[2] .= "<TH >".$dbitoMes."</TH>" ;
		$res[2] .= "</TR>" ;

		$res[2] .= "<TR>" ;
		$res[2] .= "<TH colspan='2' >SALDO</TH>" ;
		$res[2] .= "<TH >".($_Valores[0]->soma - $dbitoMes)."</TH>" ;
		$res[2] .= "</TR>" ;

		$res[2] .= "</TABLE>" ;

		$botao = new MGeraBotoes ;
		$res[3] .= $botao->gerar("ATUALIZAR RESUMO POR TÍTULOS|acao|G|2|".$mref."#RELAT&Oacute;RIO FINANCEIRO|acao|R|0") ;

	}
	catch (Exception $e)
	{
		$_var = MformTryCache::objReturn($e) ;
		TTransaction::rollback() ;
		return $_var;
	}
	return $res ;
}

function atualizarPagante($textoGrava)
{
	try
	{
		TTransaction::open($_SESSION['infoCon']);
		TTransaction::setLogger(new TLoggerTXT('/tmp/atualizarPagante.txt'));

		$registro = explode("**#",$textoGrava) ;
		for($i = 0 ; $i < count($registro)-1 ; $i++)
		{
			$campos = explode("#",$registro[$i]) ;
			$a = new pagante ;
			for($j = 0 ; $j < count($campos) ; $j++)
			{
				$nv = explode("|",$campos[$j]) ;
				if ($nv[0] == 'mesref')
					$str = $nv[1] ;
				$a->$nv[0] = $nv[1] ;
			}
			$a->alterarIncluir ;
			if ($a->resp[0] == 'erro')
				throw new Exception($a->resp[1]) ;
		}
		TTransaction::close() ;
	}
	catch (Exception $e)
	{
		$_var = MformTryCache::objReturn($e) ;
		TTransaction::rollback() ;
		return $_var;
	}
	return buscaEdicaoConta($str) ;
}

$sajax_request_type = "POST";
sajax_init();
sajax_export("buscaEdicaoConta", "validaData", "contasInicio", "atualizarPagante");
sajax_handle_client_request();
